__kernel void pi_calc(__global float* arrayDevice,
                     float delta,
                     int log_size)
{
    float temp; // Private memory!
    int gid = get_global_id(0);

    float x = ((float) gid + 0.5)*delta;
    arrayDevice[gid] = 4.0/(1.0 + x*x);
    barrier(CLK_GLOBAL_MEM_FENCE);

    // Apply the prefix-sum algorithm:
    // Iterate over log2(size)
    int pow = 1;
    for (int i=1; i<=log_size; i++){
        temp = arrayDevice[gid];
        if (gid >= pow){
            // Adding backwards, gid >= 2^i
            temp += arrayDevice[gid - pow];
        }
        arrayDevice[gid] = temp;
        barrier(CLK_GLOBAL_MEM_FENCE);
        // Multiply by 2
        pow = pow<<1;
    }
}
